﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.Entity;

using FinalProject.DAL.Mapping;
using FinalProject.DAL.Model;
using System.Data;

namespace FinalProject.BL
{
    public class BaseBL<T> where T : class
    {
        protected static readonly InvoiceModelContext dalContext = new InvoiceModelContext(Utilities.ConnectionString);

        public static void Insert()
        {
            try
            {

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static IList<T> GetAll()
        {
            var result = dalContext.CreateObjectSet<T>().ToList();
            return result;
        }

        public static T LoadById(Guid Id)
        {
            object entity = null;
            string setName = string.Concat(dalContext.DefaultContainerName, ".", dalContext.CreateObjectSet<T>().EntitySet.Name);
            var key = new EntityKey(setName, "Id", Id);
            dalContext.TryGetObjectByKey(key, out entity);
            if (entity != null)
                return entity as T;
            else
                return null;
        }

        public static T LoadByCode(string code)
        {
            try
            {
                object entity = null;
                string setName = string.Concat(dalContext.DefaultContainerName, ".", dalContext.CreateObjectSet<T>().EntitySet.Name);
                var key = new EntityKey(setName, "Code", code);
                dalContext.TryGetObjectByKey(key, out entity);
                if (entity != null)
                    return entity as T;
                else
                    return null;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static IList GetListCode()
        {
            var result = dalContext.CreateObjectSet<T>()
                                    .SelectValue<string>("it.Code")
                                    .ToList();
            return result;
        }

        //public static object GetFieldValue(Guid Id)
        //{
        //    try
        //    {

        //    }
        //    catch (Exception)
        //    {
                
        //        throw;
        //    }
        //}
    }
}
